home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 May: Tool Chest / Developer CD Series May 1996 (Tool Chest) (Apple Computer) (1996).iso / Sample Code / Snippets / QuickDraw / Marquee ƒ / marquee.c.save < prev    next >
Encoding:
Text File  |  1992-07-15  |  3.8 KB  |  167 lines  |  [TEXT/MPS ]

  1. #include <QuickDraw.h>
  2. #include <Events.h>
  3. #include <Fonts.h>
  4. #include <Menus.h>
  5. #include <Windows.h>
  6. #include <Packages.h>
  7.  
  8. Resume() {
  9.     ExitToShell();
  10. }
  11.     
  12. main () {
  13.     auto    EventRecord        event;
  14.     auto    WindowRecord    window;
  15.     auto    Rect            bounds,
  16.                             rect;
  17.     auto    Point            start;
  18.     
  19.     /*
  20.     ** initialize the macintosh
  21.     */
  22.     InitGraf((Ptr) &qd.thePort);
  23.     InitFonts();
  24.     InitWindows();
  25.     InitMenus();
  26.     TEInit();
  27.     InitDialogs((ResumeProcPtr) Resume);
  28.     InitCursor();
  29.         
  30.     SetRect(&bounds, 50, 50, 500, 300);
  31.     NewWindow((Ptr)&window, &bounds, "\pApple Computer Inc.", true, noGrowDocProc, (WindowPtr)-1L, false, 0L);
  32.     SetPort((GrafPtr)&window);
  33.     
  34.     while (true) {
  35.         GetNextEvent(everyEvent, &event);
  36.         switch (event.what) {
  37.             case mouseDown :
  38.                 start = event.where;
  39.                 GlobalToLocal(&start);
  40.                 TrackMarquee(start, &rect);
  41.                 break;
  42.             case keyDown :
  43.                 ExitToShell();
  44.                 break;
  45.         }
  46.     }
  47. }
  48.  
  49. /*
  50. ** Description
  51. **        TrackMarquee will display a marquee similar to the 
  52. **        selection rectangle tool in MacPaint™.
  53. **
  54. ** Parameters
  55. **        start        : the local coordinates where the mouse down occured.
  56. **        resultRect    : the final rectangle that was selected.
  57. */
  58.  
  59. #define    TICKDELAY    2
  60.  
  61. TrackMarquee(start, resultRect)
  62.     Point    start;
  63.     Rect    *resultRect;
  64. {    
  65.     /*
  66.     ** there are fifteen patterns defined here 
  67.     ** each one eight bytes long starting at :
  68.     ** patterns[0], patterns[1], patterns[2], patterns[3],
  69.     ** patterns[4], patterns[5], patterns[6], patterns[7]
  70.     */
  71.     static    unsigned char    patterns[] = {
  72.         0xF8, 0xF1, 0xE3, 0xC7, 0x8F, 
  73.         0x1F, 0x3E, 0x7C, 0xF8, 0xF1, 
  74.         0xE3, 0xC7, 0x8F, 0x1F, 0x3E
  75.     };
  76.     
  77.     auto        Point        mouse;        /* the current mouse location */
  78.     register    short        index;        /* the index of the current patterns array */
  79.     auto        Rect        nowRect,    /* the current rectangle to be framed */
  80.                             thenRect;    /* the last rectangle to be framed */
  81.     auto        long        nowTicks,    /* the current tick count */
  82.                             thenTicks;    /* the last tick count */
  83.     auto        PenState    penState;    /* the saved pen state on entry to procedure */
  84.  
  85.     
  86.     thenTicks = 0;
  87.     index = 0;
  88.     
  89.     GetPenState(&penState);
  90.     PenMode(patXor);
  91.     
  92.     PenPat(&patterns[index]);
  93.     SetRect(&nowRect, start.h, start.v, start.h, start.v);
  94.     FrameRect(&nowRect);
  95.     thenRect = nowRect;
  96.     
  97.     while (StillDown()) {
  98.         nowTicks = TickCount();
  99.         GetMouse(&mouse);
  100.         SetMobiusRect(&nowRect, start.h, start.v, mouse.h, mouse.v);
  101.         if (((thenTicks + TICKDELAY) < nowTicks ? thenTicks = nowTicks, true : false) || (!EqualRect(&nowRect, &thenRect))) {
  102.             FrameRect(&thenRect);
  103.             index = index < 7 ? index + 1 : 0;
  104.             PenPat(&patterns[index]);
  105.             FrameRect(&nowRect);
  106.             thenRect = nowRect;
  107.         }
  108.     }
  109.     FrameRect(&thenRect);
  110.     
  111.     SetPenState(&penState);
  112.     *resultRect = thenRect;
  113. }
  114.  
  115. IdleMarquee(rect)
  116.     Rect    *rect;
  117. {    
  118.     /*
  119.     ** there are fifteen patterns defined here 
  120.     ** each one eight bytes long starting at :
  121.     ** patterns[0], patterns[1], patterns[2], patterns[3],
  122.     ** patterns[4], patterns[5], patterns[6], patterns[7]
  123.     */
  124.     static    unsigned char    patterns[] = {
  125.         0xF8, 0xF1, 0xE3, 0xC7, 0x8F, 
  126.         0x1F, 0x3E, 0x7C, 0xF8, 0xF1, 
  127.         0xE3, 0xC7, 0x8F, 0x1F, 0x3E
  128.     };
  129.  
  130.     static    short        index;        /* the index of the current patterns array */
  131.     static    long        nowTicks,    /* the current tick count */
  132.                         thenTicks;    /* the last tick count */
  133.     auto    PenState    penState;    /* the saved pen state on entry to procedure */
  134.  
  135.     GetPenState(&penState);
  136.     PenMode(patXor);
  137.  
  138.     nowTicks = TickCount();
  139.     if ((thenTicks + TICKDELAY) < nowTicks ? thenTicks = nowTicks, true : false)) {
  140.         index = index < 7 ? index + 1 : 0;
  141.         PenPat(&patterns[index]);
  142.         FrameRect(rect);
  143.     }
  144.  
  145.     SetPenState(&penState);
  146. }
  147.  
  148. SetMobiusRect(rect, left, top, right, bottom)
  149.     Rect    *rect;
  150.     short    left, top, right, bottom;
  151. {
  152.     if (left > right) {
  153.         rect->left = right;
  154.         rect->right = left;
  155.     } else {
  156.         rect->left = left;
  157.         rect->right = right;
  158.     }
  159.     if (top > bottom) {
  160.         rect->top = bottom;
  161.         rect->bottom = top;
  162.     } else {
  163.         rect->top = top;
  164.         rect->bottom = bottom;
  165.     }
  166. }
  167.